var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        coupon: {},
        goodsList: [],
        imgUrl: "",
        texts: ['亲，您已经领取过了哦！','领取成功'],
        text: '亲，您已经领取过了哦！',
        isShow:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //获取优惠券信息
        var that = this;
        wx.request({
            url: app.url + "?controller=api_coupon&action=couponInfo",
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            },
            data:{
                couponId: options.id
            },
            success: function (data) {
                console.log(data);
                that.setData({
                    coupon: data.data.result,
                    id: options.id,
                });
            }
        })
        this.goodsList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      var that = this;
      return {
        title: '快来领取吧~',
        path: 'pages/account/discount/couponPage/couponPage?id=' + that.data.id,
      }
    },

    goodsList: function () {
        var that = this;
        wx.request({
            url: app.url + "?controller=api_coupon&action=recommend",
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            },
            success: function (data) {

                that.setData({
                    goodsList: data.data.result,
                    imgUrl: app.urlimg
                });
            }
        })
    },

    //领取优惠券
    received: function () {
        var that = this;
        wx.request({
            url: app.url + "?controller=api_coupon&action=receive",
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            },
            data:{
                couponId: that.data.coupon.id
            },
            success: function (data) {
                that.data.texts[0] = that.data.texts[1];
                that.setData({
                    text : that.data.texts[1],
                    isShow: 0
                });
            }
        })
    },
    toCard:function() {
        wx.navigateTo({
          url: '/pages/account/discount/index?id=3',
        })
    },

    toDetail:function(e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/index/detail/index?id='+id,
        })
    }
})